/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import com.lineage.server.PacketCreate;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.utils.Random;

/**
 * Class <code>PurifyStone</code> 黑暗精灵魔法:提炼魔石 (5/0).
 * 可以牺牲魔力来改变“黑魔石”的性质，提炼失败的话，石头就会消失的无影无踪。
 * “黑魔石”的变化一共4个等级，变质后的“黑魔石”能当作魔法的触媒，或是当成制作道具的材料。（黑暗妖精15级试炼取得。）
 * 
 * @author jrwz
 * @version 2012-7-27上午09:54:06
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class PurifyStone extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new PurifyStone();
    }

    /** 黑暗精灵魔法:提炼魔石 (5/0). */
    private PurifyStone() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            final L1ItemInstance item = pc.getInventory().getItem(targetID);
            if (item != null) {
                final int dark = (int) (10 + (pc.getLevel() * 0.8) + (pc
                        .getWis() - 6) * 1.2);
                final int brave = (int) (dark / 2.1);
                final int wise = (int) (brave / 2.0);
                final int kayser = (int) (wise / 1.9);
                final int run = Random.nextInt(100) + 1;
                final int oldItem = item.getItem().getItemId();
                switch (oldItem) {
                    case 40320: // 一级黑魔石
                        pc.getInventory().removeItem(item, 1);
                        if (dark >= run) {
                            pc.getInventory().storeItem(40321, 1);
                            pc.sendPackets(new S_ServerMessage(403, "$2475")); // 获得%0%o 。
                        } else {
                            pc.sendPackets(new S_ServerMessage(280)); // \f1施咒失败。
                        }
                        break;
                    case 40321: // 二级黑魔石
                        pc.getInventory().removeItem(item, 1);
                        if (brave >= run) {
                            pc.getInventory().storeItem(40322, 1);
                            pc.sendPackets(new S_ServerMessage(403, "$2476")); // 获得%0%o 。
                        } else {
                            pc.sendPackets(new S_ServerMessage(280));// \f1施咒失败。
                        }
                        break;
                    case 40322: // 三级黑魔石
                        pc.getInventory().removeItem(item, 1);
                        if (wise >= run) {
                            pc.getInventory().storeItem(40323, 1);
                            pc.sendPackets(new S_ServerMessage(403, "$2477")); // 获得%0%o 。
                        } else {
                            pc.sendPackets(new S_ServerMessage(280));// \f1施咒失败。
                        }
                        break;
                    case 40323: // 四级黑魔石
                        pc.getInventory().removeItem(item, 1);
                        if (kayser >= run) {
                            pc.getInventory().storeItem(40324, 1);
                            pc.sendPackets(new S_ServerMessage(403, "$2478")); // 获得%0%o 。
                        } else {
                            pc.sendPackets(new S_ServerMessage(280));// \f1施咒失败。
                        }
                        break;
                    default:
                        PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                        break;
                }
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
